package com.example.user.controller;

import com.example.common.api.ResultUtil;
import com.example.common.validator.Insert;
import com.example.common.validator.Update;
import com.example.user.model.vo.AddressVO;
import com.example.user.service.IAddressService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.concurrent.ExecutionException;

@RestController
@Slf4j
@RequestMapping("/api/address")
@Validated
public class AddressController {

    @Resource
    private IAddressService addressService;

    @RequestMapping(value = "/create_address", method = RequestMethod.POST)
    public Mono<ResultUtil> createAddress(@RequestBody @Validated(Insert.class) AddressVO addressVO){
        return addressService.createAddress(addressVO);
    }

    @RequestMapping(value = "/update_address", method = RequestMethod.PUT)
    public ResultUtil updateAddress(@RequestBody @Validated(Update.class) AddressVO addressVO){
        return addressService.updateAddress(addressVO);
    }

    @RequestMapping(value = "/del_address/{id}", method = RequestMethod.POST)
    public ResultUtil deleteAddress(@PathVariable(required = false) Long id){
        return addressService.deleteAddress(id);
    }

    @RequestMapping(value = "/get_address_by_page", method = RequestMethod.GET)
    public Mono<ResultUtil> getAddressByPage(@RequestParam int pageIndex,
                                       @RequestParam int pageSize) throws ExecutionException, InterruptedException {
        return addressService.getAddressByPage(pageIndex, pageSize);
    }

    @RequestMapping(value = "/get_address_by_id/{id}", method = RequestMethod.GET)
    public ResultUtil getAddressById(@PathVariable(value = "id") Long id) {
        return addressService.getAddressById(id);
    }

    @RequestMapping(value = "/get_address_by_user_id", method = RequestMethod.GET)
    public Mono<ResultUtil> getAddressByUserId() {
        Mono<ResultUtil> mono = addressService.getAddressByUserId();

        return mono;
    }
}
